﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DevDL.UserLimit.Models;
using DevDL.UserLimit.DBUtility;
using DevDL.UserLimit.IDAL;

namespace DevDL.UserLimit.SQLServerDAL
{
    public class FunctionDAL : IFunctionDAL
    {
        public DataSet GetFunctions()
        {
            string sql = "SELECT [Id] ,[PId],[Name],[ControlId],[SystemType] FROM [T_Function]";
            return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql);
        }
        public List<Function> GetTopFunction()
        {
            List<Function> list = new List<Function>();
            string sql = "SELECT [Id],[PId],[Name],[ControlId] FROM [T_Function] WHERE [PId]=-1";
            using (SqlDataReader reader=SqlHelper.ExecuteReader(SqlHelper.GetConnection(),CommandType.Text,sql))
            {
                while (reader.Read())
                {
                    Function f = new Function();
                    f.Id = Convert.ToInt32(reader["Id"]);
                    f.Name = reader["Name"].ToString();
                    f.PId = Convert.ToInt32(reader["PId"]);
                    f.ControlId = reader["ControlId"].ToString();
                    list.Add(f);
                }
                return list;
            }
        }
        public List<Function> GetFunctionByPId(int pid)
        {
            List<Function> list = new List<Function>();
            string sql = "SELECT [Id],[PId],[Name],[ControlId] FROM [T_Function] WHERE [PId]="+pid;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql))
            {
                while (reader.Read())
                {
                    Function f = new Function();
                    f.Id = Convert.ToInt32(reader["Id"]);
                    f.Name = reader["Name"].ToString();
                    f.PId = Convert.ToInt32(reader["PId"]);
                    f.ControlId = reader["ControlId"].ToString();
                    list.Add(f);
                }
                return list;
            }
        }
    }
}
